package com.ptswitch.material.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hxzon.util.NumberFormatUtil;
import org.hxzon.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.ptswitch.material.dao.MaterialMapper;
import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.service.MaterialService;

@Service
public class MaterialServiceImpl implements MaterialService {

    private static final Logger logger = LoggerFactory.getLogger(MaterialServiceImpl.class);
    @Autowired
    private MaterialMapper materialMapper;

    @Override
    @Async
    public void testSpringAsync() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long count = countAll();
        logger.debug("testSpringAsync:" + count);
    }

    @Override
    @Scheduled(fixedRate = 10000)
    public void testSpringScheduled() {
        long count = countAll();
        logger.debug("testSpringScheduled:" + count);
    }

    @Override
    public void saveAll(Collection<Material> newMaterials, Collection<Material> oldMaterials, Collection<Material> updateMaterials) {
        for (Material m : newMaterials) {
            materialMapper.add(m);
        }
        for (Material m : oldMaterials) {
            materialMapper.delete(m);
        }
        for (Material m : updateMaterials) {
            materialMapper.add(m);
        }
    }

    @Override
    public void delete(Material model) {
        if (!model.getBomMaterials().isEmpty()) {
            return;
        }
        // uploadDao.deleteAll(model.getUploads());
        materialMapper.delete(model);
    }

    @Override
    public Material findById(long id, int init) {
        Material material = materialMapper.findById(id);
        return material;
    }

    @Override
    public Material findByCode(String code) {
        return materialMapper.findByCode(code);
    }

    @Override
    public List<Material> findByPcbDecal(String pcbDecal) {
        return materialMapper.findByPcbDecal(pcbDecal);
    }

    @Override
    public boolean existPcbDecal(String pcbDecal) {
        long count = materialMapper.countByPcbDecal(pcbDecal);
        // logger.debug("{}",count);
        return count > 0;
    }

    @Override
    public void save(Material model) {
        String state = model.getState();
        if (state == null || state.isEmpty()) {
            state = "一般";
        }
        if (state.equals("推荐") || state.equals("一般") || state.equals("不推荐") || state.equals("停用") || state.equals("未发布")) {

        } else {
            state = state + "描述不规范，请检查";
        }
        model.setState(state);
        materialMapper.update(model);
    }

    @Override
    public void saveAll(List<Material> models) {
        for (Material model : models) {
            save(model);
        }
    }

    private Pattern p = Pattern.compile(".*-(\\d*)-.*");

    @Override
    public String emptyCode(String code) {
        code = StringUtil.escapeSql(code);
        String lastCode = materialMapper.emptyCode(code);//code-%
        logger.debug("get empty code:{}", lastCode);
        Matcher m = p.matcher(lastCode);
        try {
            m.matches();
            int index = Integer.parseInt(m.group(1)) + 1;
            return code + "-" + NumberFormatUtil.format(index, "000") + "-0";
        } catch (RuntimeException e) {
            logger.error("get empty code fail", e);
        }
        return code;
    }

    public String valid(Material model) {
        return null;
    }

    @Override
    public Material findById(long id) {
        return null;
    }

    @Override
    public List<Material> findLike(String like, long first, long size) {
        return null;
    }

    @Override
    public long countLike(String like) {
        return 0;
    }

    @Override
    public void deleteAll() {
    }

    @Override
    public List<Material> findAll() {
        return null;
    }

    @Override
    public void deleteAll(Collection<Material> materials) {

    }

    @Override
    public List<Material> findLikeByExample(Material example, long first, long size) {
        return null;
    }

    @Override
    public long countLikeByExample(Material example) {
        return 0;
    }

    @Override
    public List<Material> findAll(long first, long size) {
        return null;
    }

    @Override
    public long countAll() {
        return 0;
    }

}
